package com.tan.collection.set.treeSet;

import java.util.Objects;

public class Item implements Comparable<Item>{
    private String description;
    private int partNumber;

    /**
     * Constructs an item.
     * @param aDescription
     *          the item`s description
     * @param aPartNumber
     *          the item`s part number
     * */

    public Item(String aDescription, int aPartNumber) {
        this.description = aDescription;
        this.partNumber = aPartNumber;
    }
    /**
     * Gets the description of this item.
     * @return the description.
     * */

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return "Item{" +
                "description='" + description + '\'' +
                ", partNumber=" + partNumber +
                '}';
    }
/**
 * 重写equals方法。
 * this指当前实例
 * */
    @Override
    public boolean equals(Object otherObject) {
        if (this == otherObject)return true;
        //判断当前实例与传入参数是否为真。
        if (this == null)return false;
        //判断当前实例是否为空。
        if (getClass() != otherObject.getClass())return false;
        //返回此对象的运行时类
        Item other = (Item) otherObject;
        //强转赋值。
        return Objects.equals(description,other.description) && partNumber == other.partNumber;
        //判断描述和组成部分是否相等
    }
/**
 *
 * */
    @Override
    public int hashCode() {
        return Objects.hash(description,partNumber);
    }

    @Override
    public int compareTo(Item other) {
        int diff = Integer.compare(partNumber,other.partNumber);
        return diff != 0 ? diff : description.compareTo(other.description);
    }

}
